package com.squirrel.michale;

import java.util.HashMap;
import java.util.Map;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/10 11:18 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class AuthenticationManager {

    int timeToLive;
    Map<String, Integer> tokenMap;

    public AuthenticationManager(int timeToLive) {
        this.timeToLive = timeToLive;
        this.tokenMap = new HashMap<String, Integer>();
    }

    public void generate(String tokenId, int currentTime) {
        tokenMap.put(tokenId, currentTime + timeToLive);
    }

    public void renew(String tokenId, int currentTime) {
        if (tokenMap.getOrDefault(tokenId, 0) > currentTime) {
            tokenMap.put(tokenId, currentTime + timeToLive);
        }
    }

    public int countUnexpiredTokens(int currentTime) {
        int unexpired = 0;
        for (int time : tokenMap.values()) {
            if (time > currentTime) {
                unexpired++;
            }
        }
        return unexpired;
    }
}
